package servlets.customer;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import pojo.Colors;
import pojo.Images;
import pojo.Product;
import pojo.Sizes;
import services.ServiceSearch;

public class ServletSearchByPrice extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        ServiceSearch serviceSearch = new ServiceSearch();
        try {

            int price1 = Integer.parseInt(request.getParameter("price1"));
            int price2 = Integer.parseInt(request.getParameter("price2"));
            

            List<Product> products = serviceSearch.searchByPrice(price1,price2);

            String html = "";
            if (products.isEmpty()) {

                html += "<center>"
                        + "<h4>Sorry, no products are found</h4>"
                        + "</center>";

            } else {

                html += "<ul>\n";

                for (Product product : products) {

                    int productId = product.getId();
                    String productName = product.getName();
                    String productBrand = product.getBrand();
                    int productPrice = product.getPrice();

                    List<Colors> productColorsList = product.getColorsList();
                    String productColors = "";
                    if (!productColorsList.isEmpty()) {
                        productColors = "Colors: ";
                        for (Colors colors : productColorsList) {
                            productColors += colors.getColor() + " ";
                        }
                    }

                    List<Sizes> productSizesList = product.getSizesList();
                    String productSizes = "";
                    if (!productSizesList.isEmpty()) {
                        productSizes = "Sizes: ";
                        for (Sizes sizes : productSizesList) {
                            productSizes += sizes.getSize() + " ";
                        }
                    }

                    List<Images> productImagesList = product.getImagesList();
                    String productImage = "";
                    if (!productSizesList.isEmpty()) {
                        productImage = productImagesList.get(0).getLocation();
                    }

                    html += "<li>\n"
                            + "<div class=\"product\">\n"
                            + "<a onclick=\"viewProduct(" + productId + ")\" class=\"info\">\n"
                            + "<span class=\"holder\">\n"
                            + "<img src=\"" + productImage + "\" alt=\"\" />\n"
                            + "<span class=\"book-name\">" + productName + "</span>\n"
                            + "<span class=\"author\">" + productBrand + "</span>\n"
                            + "<span class=\"description\">\n"
                            + productSizes + "\n"
                            + "<br/>\n"
                            + productColors + "\n"
                            + "<br/></span>\n"
                            + "</span>\n"
                            + "</a>\n"
                            + "<a class=\"buy-btn\">BUY NOW <span class=\"price\"><span class=\"low\">$</span>" + productPrice + "<span class=\"high\">00</span></span></a>\n"
                            + "</div>\n"
                            + "</li>";
                }

                html += "</ul> ";

            }

            out.print(html);

        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
